package com.lanou.sfu.songforu.utils.net;
import android.app.ProgressDialog;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.datas.RecommendHostSongsDatas;
import com.lanou.sfu.songforu.datas.RecommendPlayDatas;
import com.lanou.sfu.songforu.datas.RecommendRadioDatas;
import com.lanou.sfu.songforu.datas.RecommendSongListDatas;
import com.lanou.sfu.songforu.datas.SongSearch;

import org.json.JSONObject;

/**
 * Created by 陈丰尧 on 2015/10/21.
 * 网络拉取得工具类
 */
public class HttpHelper implements NetDataWhat {
    private RequestQueue requestQueue;//请求队列
    private NetDataCallBack netDataCallBack;//返回数据的接口

    public HttpHelper(NetDataCallBack netDataCallBack) {
        this.netDataCallBack = netDataCallBack;
        requestQueue = MyQueue.getInstance().getVolleyQueue();
    }

    /**
     * 首页 数据接口
     */
    public void getMainPage() {
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.plaza.getFocusPic&format=json&num=6";
        getNetData(path,WHAT_MAIN_PAGE);
    }

    public void getData1(String args1){
        String pata = "asdfasdf?"+args1;

    }
    /**
     * 推荐歌单 数据接口
     */
    public void getQuestionPage(){
        String path = " http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.diy.getHotGeDanAndOfficial&format=json&num=6";
        getNetData(path,WHAT_QUESTION);
    }

    /**
     * 热门歌曲 数据接口
     * @parampath
     * @paramwhat
     */

    public void getHostPage(){
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.song.getEditorRecommend&format=json&num=6";
        getNetData(path,WHAT_HOSTSONG);
    }

    /**
     * 电台数据接口
     * @parampath
     * @paramwhat
     */

    public void getRadioPage(){
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.radio.getRecommendRadioList&format=json&num=6";
        getNetData(path,WHAT_RADIO);
    }

    public void getSearch(String url){
        String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.search.catalogSug&format=json&query="+ url;
        getNetData(path,WHAT_SEARCH);
    }

    public void getSearchHot(){
        String path = "http://api.dongting.com/misc/sug/billboard?uid=866058021842951&f=f3040&v=v8.2.0.2015091720&app=ttpod&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&hid=1135212123029932&s=s200&alf=alf203200&imsi=460078098497828&tid=0&net=2&size=20";
        getNetData(path,WHAT_SEARCH_HOT);
    }


    //网络数据拉取
    private void getNetData(String path, final int what) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(path,
                null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                /**成功，调用接口的成功方法
                 * @pargm response 解析之后的数据实体类
                 */
                netDataCallBack.netSuccess(what,resolution(what,response));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //失败，调用接口的失败方法
                netDataCallBack.netError(what);
            }
        });
        requestQueue.add(jsonObjectRequest);//加入到请求队列
    }

    //解析方法，将Json数据解析成实体类
    private Object resolution(int what,JSONObject response){
        Object object = null;
        switch (what){
            case WHAT_MAIN_PAGE:
                object = JSON.parseObject(response.toString(), RecommendPlayDatas.class);
                break;
            case WHAT_QUESTION:
                object = JSON.parseObject(response.toString(), RecommendSongListDatas.class);
                break;
            case WHAT_HOSTSONG:
                object = JSON.parseObject(response.toString(), RecommendHostSongsDatas.class);
                break;
            case WHAT_RADIO:
                object = JSON.parseObject(response.toString(), RecommendRadioDatas.class);
                break;
            case WHAT_SEARCH:
                object = JSON.parseObject(response.toString(), SongSearch.class);
        }
            return object;
    }



}
